<template>
  <el-container>
    <el-main>
      <h2>知识库管理</h2>
      <div style="display: flex; align-items: center; gap: 10px; margin-bottom: 20px;">
        <el-input v-model="searchKeyword" placeholder="搜索问题" style="width: 300px;" />
        <el-button type="primary" @click="searchFaqs">搜索</el-button>
        <el-button @click="openAddDialog">新增FAQ</el-button>
      </div>
      <el-table :data="faqs" border>
        <el-table-column prop="question" label="问题" />
        <el-table-column prop="answer" label="答案" />
        <el-table-column prop="category" label="分类" />
        <el-table-column label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.isActive === 1 ? 'success' : 'danger'">
              {{ scope.row.isActive === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" @click="editFaq(scope.row)">编辑</el-button>
            <el-button size="small" @click="deleteFaq(scope.row.id)">删除</el-button>
            <el-button size="small" @click="toggleStatus(scope.row.id)">
              {{ scope.row.isActive === 1 ? '禁用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          layout="prev, pager, next"
          :total="total"
          :page-size="pageSize"
          :current-page="pageNum"
          @current-change="handlePageChange"
          style="margin-top: 20px;"
      />


    </el-main>

    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="dialogVisible" title="编辑FAQ">
      <el-form :model="currentFaq" label-width="80px">
        <el-form-item label="问题">
          <el-input v-model="currentFaq.question" />
        </el-form-item>
        <el-form-item label="答案">
          <el-input v-model="currentFaq.answer" type="textarea" />
        </el-form-item>
        <el-form-item label="分类">
          <el-input v-model="currentFaq.category" />
        </el-form-item>
        <el-button @click="saveFaq">保存</el-button>
      </el-form>
    </el-dialog>
  </el-container>
</template>


<script setup>
import { ref } from 'vue';
import axios from 'axios';

const faqs = ref([]);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);
const searchKeyword = ref('');
const dialogVisible = ref(false);
const currentFaq = ref({});

function fetchFaqs(page = 1) {
  let url = `http://localhost:9998/ai-customerservice/faq/page?pageNum=${page}&pageSize=${pageSize.value}`;
  if (searchKeyword.value) {
    url = `http://localhost:9998/ai-customerservice/faq/search?keyword=${searchKeyword.value}&pageNum=${page}&pageSize=${pageSize.value}`;
  }

  axios.get(url).then(res => {
    faqs.value = res.data.records;
    total.value = res.data.total;
    pageNum.value = page;
  });
}

function handlePageChange(page) {
  fetchFaqs(page);
}

function searchFaqs() {
  fetchFaqs(1);
}

function openAddDialog() {
  currentFaq.value = {};
  dialogVisible.value = true;
}

function editFaq(faq) {
  currentFaq.value = { ...faq };
  dialogVisible.value = true;
}

function saveFaq() {
  if (currentFaq.value.id) {
    axios.put(`http://localhost:9998/ai-customerservice/faq/${currentFaq.value.id}`, currentFaq.value);
  } else {
    axios.post('http://localhost:9998/ai-customerservice/faq', currentFaq.value);
  }
  dialogVisible.value = false;
  fetchFaqs(pageNum.value);
}

function deleteFaq(id) {
  axios.delete(`http://localhost:9998/ai-customerservice/faq/${id}`).then(() => {
    fetchFaqs(pageNum.value);
  });
}

function toggleStatus(id) {
  axios.post(`http://localhost:9998/ai-customerservice/faq/toggle/${id}`).then(() => {
    fetchFaqs(pageNum.value);
  });
}

// 初始加载第一页
fetchFaqs();
</script>

